/**
 * Created by yangfan on 2016/10/13.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices"],function(app,i18n,httpService){
    app.controller('myCommentCtrl', function($scope,$state) {
        $scope.i18n = i18n;
        $scope.everPageShow = [30,50,100];
        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.tableTitle = ["主题","评论时间"];
        $scope.titleNavation = [
            {title:"我评论的",type:"myComment"},
            {title:"评论我的",type:"commented"},
        ];
        $scope.isShowCurrent = [];
        $scope.isShowCurrent[0] = true;
        $scope.isMyComment = true;
        $scope.byComment = false;
        //待切换加载数据
        $scope.checkTitle = function(type,index){
            $scope.isShowCurrent = [];
            $scope.isShowCurrent[index] = true;
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10;
            $scope.paginationConf.itemsPerPage = 10;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            if(index == 0){
                $scope.isMyComment = true;
                $scope.byComment = false;
                myComment()
            }else if(index ==1){
                $scope.isMyComment = false;
                $scope.byComment = true;
                getCommentForMe()
            }
        };

        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";

        $scope.dialogModel = {
            content:"您确定要清空所有记录吗?",
            isShow:false,
            align:"left",
            confirm:function(){
                clearComment();
            }
        }

        $scope.listParams = {
            pageNo:1,
            pageSize:10
        };
        $scope.clearAllComment = function(){
            $scope.dialogModel.isShow = true ;
        };
        function getPageModel (){
            // 起始页面1 才重新渲染分页
            if($scope.listParams.pageNo!= 1){
                return
            }
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength: 5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getList()
                }

            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }

        //获取 评论我的
        function getCommentForMe(){
            var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    all:false
                },
                uri:"commentForMe"
            };
            httpService.getCommentForMe(req).success(function(response){
                $scope.$apply(function(){
                	$scope.noContent = "暂时没有人评论您的问题";
                    $scope.CommentForMe = response.data.list;
                    getPageModel ()

                })
            }).error(function(){
                console.log("访问出错");
            })
        }
		//获取我评论的
		function myComment(){
            var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    all:false
                },
                uri:"myComment"
            };
            httpService.getCommentForMe(req).success(function(response){
                $scope.$apply(function(){
                	$scope.noContent = "您暂时没有评论的问题"
                    $scope.myComment = response.data.list;
                    getPageModel ()
                })
            }).error(function(){
                console.log("访问出错");
            })
        }
        function getList(){
            if($scope.isDataChecked == true){
                myComment()
            }else{
                getCommentForMe()
            }
        }

        //清空评论
        function clearComment(){
            var type;
            if($scope.isMyComment == true){
                type = 1
            }else  if($scope.byComment == true){
                type =2
            }
            var req = {
                type:"GET",
                uri:"delComment",
                data:{
                    type:type
                }
            };
            httpService.userCommentServices(req).success(function(response){
                getList()
            }).error(function(){})

        }
        getList() ;



        $scope.goDetailPage = function(item){
            if(item.type == 1){
                $state.go('blogxq',{blogid:item.fkId},{reload:true});
            }
            if(item.type == 2){
                $state.go('outIndexCtrl',{companyId:item.fkId},{reload:true});
            }
            if(item.type == 3){
                $state.go('zpdetailCtrl',{jobId:item.fkId},{reload:true});
            }
            if(item.type == 4){
                $state.go('eng.techdetail',{fileId:item.fkId},{reload:true});
            }
            if(item.type == 5){
                $state.go('eng.libraryDetail',{thesisId:item.fkId},{reload:true});
            }
            if(item.type == 8){
                $state.go('eng.vedioDetails',{courseId:item.fkId},{reload:true});
            }
            if(item.type == 6){
                $state.go('eng.newsDetail',{newsId:item.fkId},{reload:true});
            }
        };

        //改变每页预览条数
        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            getList() ;
        }
    })
});